﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Finance.DTO.IncomeManage;
using Entity.Finance.DTO.PayAccount;
using Entity.Finance.DTO.PayAccountItem;
using Entity.Finance.Entity;
using Entity.Finance.View.PayAccount;
using Entity.Finance.View.PayAccountItem;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class PayAccountService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public PayAccountService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<PayAccountView>>> GetPayAccountList(PayAccountSearchDTO model)
        {
            //筛选
            var query = _context.PayAccountView
                .WhereIF(a => a.Code.Contains(model.Code), !string.IsNullOrEmpty(model.Code))
                .WhereIF(a => a.SupplierCode.Contains(model.Supplier) || a.SupplierName.Contains(model.Supplier), !string.IsNullOrEmpty(model.Supplier))
                ;

            //返回列表
            var list = await query
                //.OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<PayAccountView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }
        
        /**
         * 获取子单列表
         */
        public async Task<RequestResponseModel<PageResult<PayAccountItemView>>> GetPayAccountItemList(PayAccountItemSearchDTO model)
        {
            //筛选
            var query = _context.PayAccountItemView
                .Where(a=>a.PayAccountId==model.Id)
                .WhereIF(a => a.SupplierCode.Contains(model.Supplier) || a.SupplierName.Contains(model.Supplier), !string.IsNullOrEmpty(model.Supplier))
                .WhereIF(a => a.Status == model.Status, model.Status != null)
                ;

            //返回列表
            var list = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<PayAccountItemView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增
         */
        public async Task<RequestResponseModel<string>> AddPayAccount(PayAccountAddDTO model)
        {
            //新增数据
            var entity = _mapper.Map<PayAccountModel>(model);
            //entity.CreateTime = DateTime.Now;
            entity.Id = Guid.NewGuid().ToString();
            foreach (var item in model.Items)
            {
                var entityItem = _mapper.Map<PayAccountItemModel>(item);
                entityItem.Id = Guid.NewGuid().ToString();
                entityItem.CreateTime = DateTime.Now;
                entityItem.PayAccountId = entity.Id;
                entityItem.SupplierId = entity.SupplierId;
                _context.PayAccountItem.Add(entityItem);

            }
            //更新表
            _context.PayAccount.Add(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        }

        /**
         * 发起付款
         */
        public async Task<RequestResponseModel<string>> Pay(PayManageAddDTO model)
        {
            //校验数据
            var item = await _context.PayAccountItemView.FirstOrDefaultAsync(a => a.Id == model.Id);
            if (item == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }
            if (item.PayToDo - model.Amount < 0)
            {
                return RequestResponse.CreateResponse(400, "剩余可付款金额不足", model.Id);
            }
            //新增数据
            var entity = new PayManageModel();
            entity.CreateTime = DateTime.Now;
            entity.Id = Guid.NewGuid().ToString();

            entity.PayAccountId = item.PayAccountId;
            entity.PayAccountItemId = item.Id;
            entity.SupplierId = item.SupplierId;
            entity.Amount = model.Amount;

            //更新表
            _context.PayManage.Add(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        }
        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> DeletePayAccount(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<PayAccountModel>(_context.PayAccount, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.PayAccount.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
